﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

struct Animation
{
    public int width;
    public int height;
    public int frames;
    public int frames_per_second;
    public int Y;
    public string name;
}

namespace Avaliacao
{
    abstract class GameObject
    {
        protected Texture2D texture;
        protected Vector2 position;
        protected Rectangle collision;
        protected GameWindow Window;

        protected bool visible;
        protected bool movingRight;

        //Variáveis de animação
        protected Rectangle imageData;       //Dados da imagem que será exibida na tela do jogo
        protected Rectangle spriteImage;     //Posição da imagem na spritesheet
        float alpha;                         //Transparência da imagem
        protected Color spriteColor;         //Máscara de cor que será aplicada na imagem
        protected float rotation;            //Rotação da imagem
        protected Vector2 rotation_point;    //Ponto em que a imagem sofrerá rotação
        protected SpriteEffects effects;     //Realizar o flip da imagem (horizontal ou vertical)
        protected float depth;               //Profundidade

        public Vector2 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }

        public Rectangle Collision
        {
            get { return collision; }
            set { collision = value; }
        }

        public GameObject(Texture2D texture, GameWindow Window)
        {
            this.texture = texture;
            this.Window = Window;

            this.visible = true;
            this.alpha = 1.0f;
            this.spriteColor = new Color(1.0f * alpha, 1.0f * alpha, 1.0f * alpha);
            this.rotation = 0f;
            this.rotation_point = Vector2.Zero;
            this.depth = 0f;
            this.movingRight = true;
        }

        public abstract void Update(GameTime gameTime);

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!visible)
                return;

            spriteBatch.Draw(this.texture, this.position, Color.White);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Animation anim)
        {
            if (!visible)
                return;

            int current_frame = (int)(gameTime.TotalGameTime.TotalSeconds * anim.frames_per_second) % anim.frames;

            imageData = new Rectangle((int)position.X, (int)position.Y, anim.width, anim.height); // Posição na tela
            spriteImage = new Rectangle(current_frame * anim.width, anim.Y * anim.height, anim.width, anim.height); // Posição na spritesheete

            effects = ((movingRight) ? SpriteEffects.None : SpriteEffects.FlipHorizontally);

            spriteBatch.Draw(texture, imageData, spriteImage, spriteColor, rotation, rotation_point, effects, depth);
        }
    }
}
